
<template>
  <el-container class="home-container"
    ><!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/桔梗.png" alt="" />
        <span> 后台管理系统 </span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单 -->
        <el-menu
          background-color="#99c9ff"
          text-color="#fff"
          active-text-color="#29353a"
          unique-opened
          :collapse-transition="false"
          :collapse="isCollapse"
          router
          :default-active="activePath"
        >
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 一级菜单模板 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/'+ subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      iconsObj: {
        125: 'iconfont icon-wo',
        103: 'iconfont icon-quanxianguanli',
        101: 'iconfont icon-xiazai',
        102: 'iconfont icon-logooo_huabanfuben',
        145: 'iconfont icon-shujutongji'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created () {
    // eslint-disable-next-line no-unused-expressions
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$message.success('退出成功')
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      //   console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // console.log(res.data)
      this.menuList = res.data
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.el-header {
  background-color: #29353a;
  display: flex;
  justify-content: space-between;
  padding: 0;
  align-items: center;
  color: #fefefe;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    > img {
      height: 60px;
    }
  }
}
.el-aside {
  background-color: #99c9ff;
  .el-menu {
    border-right: 0;
  }
}
.el-main {
  background-color: #fefefe;
}
.home-container {
  height: 100%;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: rgb(1, 90, 163);
  line-height: 24px;
  font-size: 10px;
  color: floralwhite;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
